package fr.dauphine.td3.emp;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Version;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Proxy;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

@Entity 
@Table(name="emp") 
@SQLDelete(sql = "UPDATE emp SET valide = 0 where id=? and version=?")
@Where(clause = "valide <> 0")
@Proxy(lazy=false)
public class Employe implements Comparable {
	
	@Id
	@GeneratedValue(generator = "system-uuid")
	@GenericGenerator( name="system-uuid", strategy="uuid", parameters = {@Parameter (name="unsaved-value", value="undefined") })
	private String id = "";
	
	int age;
	String nom;
	String prenom;
	
	@Lob
	private byte[] photo;
	
	int valide;
	
	@Version
	long version;
	
	String login;
	String password;
	
	public Employe() {
		this("","",0);
	}
	
	public Employe(String nom, String prenom, int age) {
		this.nom = nom;
		this.prenom = prenom;
		this.age = age;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getPrenom() {
		return prenom;
	}

	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	public int getValide() {
		return valide;
	}
	
	public void setValide(int valide) {
		this.valide = valide;
	}
	
	public long getVersion() {
		return version;
	}
	
	public void setVersion(long version) {
		this.version = version;
	}

	public byte[] getPhoto() {
		return photo;
	}

	public void setPhoto(byte[] photo) {
		this.photo = photo;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null)
			return false;
		
		if (getClass() != obj.getClass())
			return false;
		
		Employe e = (Employe) obj;
		
		return nom.equals(e.getNom()) && prenom.equals(e.getPrenom()) && age == e.getAge() && id == e.getId();
	}
	
	@Override
	public int compareTo(Object o) {

		Employe e = (Employe) o;
		
		return nom.compareTo(e.getNom()) + prenom.compareTo(e.getPrenom()) + (age - e.getAge());
		
	}
	
	public int hashcode() {
		return  age * 87 + nom.hashCode() + prenom.hashCode();
	}
	
	@Override
	public String toString() {
		return "nom: " + nom + "; prenom : " + prenom + "; age: " + age;
		
	}
	
	

}
